﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _26_Flyweight
{
    /* 
     * 是所有 具有享元的超类或接口，通过这个接口，Flyweight 可以接受并作用与外部状态
     */
    abstract class Flyweight
    {
        public abstract void Opetation(int extrinsicstate);
    }

    /*
     继承超类或实现Flyweight接口，并为内部状态增加存储空间
         */
    class ConcreteFlyWeight : Flyweight
    {
        public override void Opetation(int extrinsicstate)
        {
            Console.WriteLine("具体Flyweight:" + extrinsicstate);
        }
    }
    /* 指不需要共享的Flyweight 子类 。 因为Flyweight接口共享成为可能，但他并不强制强制*/
    class UnSharedConcreteFlyweight : Flyweight
    {
        public override void Opetation(int extrinsicstate)
        {
            Console.WriteLine("不共享的具体Flyweight:" + extrinsicstate);
        }
    }

    /*
     是一个享元工厂，用来创建并管理Flyweight对象。它主要是用来确保合理的共享Flyweight,
     当用户请求一个Flyweight时，FlyweightFactory 对象提供一个已创建的实例或创建一个(如
     果不存在的话)
         */
    class FlyweightFactory
    {
        private Hashtable flyweights = new Hashtable();
        public FlyweightFactory()
        {
            flyweights.Add("X", new ConcreteFlyWeight());
            flyweights.Add("Y", new ConcreteFlyWeight());
            flyweights.Add("Z", new ConcreteFlyWeight());
        }
        public Flyweight GetFlyweight(string key)
        {
            return ((Flyweight)flyweights[key]);
        }
    }

}
